package com.wn.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wn.model.User;
import com.wn.service.IUserService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.support.HttpRequestHandlerServlet;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author yellow Docter
 * @date 2022 -06-09
 * @desc
 */
@RestController
@CrossOrigin
@RequestMapping("POI")
public class POIController {

    @Autowired
    private IUserService userService;


    /**
     * 返回一个excel 数据
     *
     * 01 拿到数据
     * 02 创建一个excel表格
     * 03 把数据 进行写入excel
     * 04 回写excel
     *
     *   需要 通过io流的操作 进行回写
     *          HttpServletRequest   获取 http提交的数据 (tomcat提供的)
     *          HttpServletResponse  给请求进行回写数据
     *                   string
     *                   byte /io 流
     *
     *     springmvc中可以直接在 处理请求的方法中中注入javaweb的组件
     *      HttpServletRequest\HttpServletResponse\HttpSession
     *
     *
     */
    @GetMapping("excel")
    public void outExcel(Integer currentPage,Integer pageSize,String userName,HttpServletResponse resp) throws IOException {
        HttpServletRequest a;
        HttpServletResponse b;

        // 创建 表格
        Workbook wb = new HSSFWorkbook();

        // 获取数据
        IPage<User> page = new Page<>(currentPage,pageSize);
        // 添加查询判断
        QueryWrapper<User> qr = new QueryWrapper<>();
        if (StringUtils.hasText(userName)){
            //给条件查询件
            // 数据库的列名
            qr.eq("user_name",userName);
        }
        IPage<User> p = userService.page(page,qr);

        //拿到分页的数据
        List<User> list = p.getRecords();

        //把数据写入excel
        Sheet sheet = wb.createSheet("学生信息");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("编号");
        row.createCell(1).setCellValue("用户名");
        row.createCell(2).setCellValue("真实姓名");
        row.createCell(3).setCellValue("学级");
        row.createCell(4).setCellValue("性别");
        row.createCell(5).setCellValue("电话");

        for (int i = 0; i < list.size(); i++) {

            User user = list.get(i);
            //把数据写入excel
            Row row1 = sheet.createRow(i + 1);
            row1.createCell(0).setCellValue(user.getId());
            row1.createCell(1).setCellValue(user.getUserName());
            row1.createCell(2).setCellValue(user.getRealName());
            row1.createCell(3).setCellValue(user.getUserLevel());
            row1.createCell(4).setCellValue(user.getSex() == 1 ? "男":"女");
            row1.createCell(5).setCellValue(user.getPhone());
        }
        //设置一个头信息   让浏览器 把回写的文件保存到 本地
        resp.setHeader("content-disposition","attachment;filename=user.xls");

        // resp的输出流
        ServletOutputStream out = resp.getOutputStream();
        //把excel保存到 输出流中
        wb.write(out);

    }

}
